package test.through.tree;

import java.math.BigDecimal;

import hyl.base.flow.chart.CNode;
import hyl.base.flow.chart.CWorkOrd;
import hyl.base.flow.chart.INode;
import hyl.base.flow.chart.IWorkOrd;
import hyl.base.flow.chart.IWorker;
import hyl.core.MyFun;

/**
 * 
 * <p>
 * className: TreeRwdCP
 * </p>
 * <p>
 * Description: 层碰奖 支持固定几层或无限层层碰，可与点碰奖相结合。 比例为 1:1, 1:2, 1:3等。
 * </p>
 * 
 * @author: zdy
 * @date: 2018年9月19日 上午9:56:05
 * @copyright: 2018 da you zou Inc. All rights reserved.
 */
public class CworkTree2DRwdCP implements IWorker {
	private final String CONSUME = "CPJ"; // 对应数据库.表.字段
	private final String NAME = "层碰奖";// 定义奖金名称
	private final String PP1 = "CP1:1";// 定义扩展属性名称
	private final BigDecimal y = BigDecimal.valueOf(0.05);// 预留: 系数
	private final int rate = 1;// 左边:右边=1:1
	private BigDecimal x = BigDecimal.valueOf(10);

	// 点碰法

	private void setRwd(CNode p, int num) {
		Creward r = new Creward();
		r.setWho(p.getId());
		r.setHowmuch(x.toPlainString());
		r.setSubject(this.NAME);
		r.setRemark("因为 (左区:右区=1:1 ,数量=" + num + ")");
		r.setTime(MyFun.getNow());
		// p.setEvent(r.getSort(), r);
	}

	@Override
	public Object run(INode node, IWorkOrd 待加工对象,Object... 其他参数) throws Exception {
		CNode parent = (CNode) node.getParent();
		Integer num = (Integer) parent.getAttr(PP1);// 已经碰过的数
		if (num == null || num == 0) {
			CNode left = (CNode) parent.getChild("位置", "L");
			CNode right = (CNode) parent.getChild("位置", "R");
			int ln = (int) left.getAttr("nodes");//
			int rn = (int) right.getAttr("nodes");
			if (ln > rn && rn > num) {
				parent.putAttr(PP1, rn);
				setRwd(parent, rn);
				return true;
			} else if (ln < rn && ln > num) {
				parent.putAttr(PP1, ln);
				setRwd(parent, rn);
				return true;
			}
		}
		return false;
	}

}
